package main

import (
	"bytes"
	"compress/gzip"
	"context"
	"fmt"
	"io"
	"log"

	"github.com/cloudwego/hertz-benchmark/perf/cpu"
)

func main() {
	ctx, finished := context.WithCancel(context.Background())
	go func() {
		defer finished()
		compress()
	}()
	usage, err := cpu.RecordUsage(ctx)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(usage.String())
}

func compress() {
	data, _ := generateRandomBytes(512 * MB)

	// compress
	var buf bytes.Buffer
	zw := gzip.NewWriter(&buf)

	_, err := zw.Write(data)
	if err != nil {
		log.Fatal(err)
	}

	if err := zw.Close(); err != nil {
		log.Fatal(err)
	}

	// decompress
	var deBuf bytes.Buffer

	zr, err := gzip.NewReader(&buf)
	if err != nil {
		log.Fatal(err)
	}

	if _, err := io.Copy(&deBuf, zr); err != nil {
		log.Fatalf("decopmpress failed: %v", err)
	}

	if err := zr.Close(); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("result:%v\n", bytes.Equal(data, deBuf.Bytes()))
}
